// 获取应用实例
const app = getApp();

// 初始化数据库
const db = wx.cloud.database();
const message = db.collection("message");
const imgs = db.collection("imgs");
const uimg = db.collection("uimg");

// pages/user/user.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    hide: false,
    theme: 1,
    // 用户信息
    userimg: "",
    username: "",
    zoneMsg: "",
    userInfo: "",
    openid: "",
    // 面板信息
    page: 0,
    hundredList: [],
    filterHundredList: [],
    msgLeng: 0,
    hunLeng: 0,
    memLeng: 0,
    stoLeng: 0,
    heartbeat_value: 0,
    // 定时器
    getHunTime: null,
    timer: null,
    // storyImg,
    // 分享
    showShare: false,
    options: [
      {
        name: "微信",
        icon: "wechat",
        openType: "share",
      },
      {
        name: "小程序码",
        icon: "weapp-qrcode",
      },
      {
        name: "复制链接",
        icon: "link",
      },
      // {
      //   name: "分享海报",
      //   icon: "poster",
      // },
      {
        name: "二维码",
        icon: "qrcode",
      },
    ],
    showErweima: false,
    showXiaochengxu: false,
    poster: false,

    userInfoMask: false,

    navHeight: app.globalData.navBarHeight,
    menuHeight: app.globalData.menuHeight,
    menuRight: app.globalData.menuRight,
    menuTop: app.globalData.menuTop,
  },

  // 获取数据
  getLengthData(openid) {
    uimg.where({ _openid: openid }).get({
      success: (res) => {
        let { hundred, memorial, heartbeat_value } = res.data[0];
        hundred = hundred.filter((i) => i.state == true);
        this.setData({
          hunLeng: hundred.length,
          memLeng: memorial.length,
          heartbeat_value
        });
      },
      fail: () => {
        wx.showToast({
          title: "获取失败",
          icon: "none",
        });
      },
    });
    message.where({ _openid: openid }).get({
      success: (res) => {
        const { myMsg } = res.data[0];
        this.setData({
          msgLeng: myMsg.length,
        });
      },
      fail: () => {
        wx.showToast({
          title: "获取失败",
          icon: "none",
        });
      },
    });
    imgs.where({ _openid: openid }).get({
      success: (res) => {
        const { myImgs } = res.data[0];
        this.setData({
          stoLeng: myImgs.length,
        });
      },
      fail: () => {
        wx.showToast({
          title: "获取失败",
          icon: "none",
        });
      },
    });
  },

  // 评分
  rate(e) {
    let { timer } = this.data;
    clearTimeout(timer);
    timer = setTimeout(() => {
      console.log("评价成功");
      wx.showToast({
        title: "评价成功",
        icon: "success",
      });
    }, 1000);
    this.setData({
      timer,
    });
  },

  // 分享开始
  share(event) {
    this.setData({
      showShare: true,
    });
  },

  onClose() {
    this.setData({
      showShare: false,
    });
  },

  onSelect(event) {
    // console.log(event);
    // this.onClose();
    switch (event.detail.name) {
      case "微信":
        this.setData({
          showShare: false,
        });
        wx.showToast({
          title: "分享给好友",
          icon: "none",
        });
        break;
      case "二维码":
        this.setData({
          showErweima: true,
          showShare: false,
        });
        break;
      case "小程序码":
        this.setData({
          showXiaochengxu: true,
          showShare: false,
        });
        break;
      case "分享海报":
        this.setData({
          poster: true,
          showShare: false,
        });
        break;
      case "复制链接":
        wx.setClipboardData({
          // data: "http://loong_cc.gitee.io/love-home",
          data: "#小程序://小本本/ycWWW213mQxZJWq",
          success: () => {
            wx.showToast({
              title: "复制成功",
            });
            this.setData({
              showShare: false,
            });
          },
        });
        break;

      default:
        wx.showToast({
          title: "开发中",
          icon: "none",
        });
        this.setData({
          showShare: false,
        });
    }
  },

  onClickHide() {
    this.setData({
      showErweima: false,
      showXiaochengxu: false,
      poster: false,
    });
  },
  // 分享结束

  theme() {
    let { theme } = this.data;
    if (theme == 1) {
      theme = 2;
      app.globalData.theme = 2;
    } else {
      theme = 1;
      app.globalData.theme = 1;
    }
    this.setData({
      theme,
    });
    wx.setStorageSync("theme", theme);
    if (app.globalData.theme == 2) {
      wx.setTabBarStyle({
        backgroundColor: "#1a1a1a",
      });
    } else if (app.globalData.theme == 1) {
      wx.setTabBarStyle({
        backgroundColor: "#a5646d",
      });
    }
    // this.getTabBar().watchBack();
  },

  checkAvatar(e) {
    const { url } = e.currentTarget.dataset;
    wx.previewImage({
      urls: [url],
    })
  },

  // showUserInfoMask() {
  //   this.setData({
  //     userInfoMask: true,
  //   });
  // },

  // closeUserInfoMask(e) {
  //   this.setData({
  //     userInfoMask: false,
  //   });
  // },

  setUser(e) {
    const that = this;
    const { openid } = that.data;
    uimg
      .where({
        _openid: openid,
      })
      .get({
        success: (res) => {
          let userList = res.data[0];
          that.setData({
            // userInfoMask: false,
            userimg: userList.avatarUrl,
            username: userList.nickname,
          });
          wx.setStorageSync("avatarUrl", userList.avatarUrl);
          wx.setStorageSync("nickname", userList.nickname);
        },
        fail: (err) => {
          console.log(err);
        },
      });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) { },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() { },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    if (typeof this.getTabBar === "function" && this.getTabBar()) {
      this.getTabBar().setData({
        selected: 3,
        theme: app.globalData.theme,
      });
    }
    this.setData({
      theme: app.globalData.theme,
      hide: app.globalData.switch,
    });
    let { zoneMsg, openid, theme } = this.data;
    if (theme == 1) {
      wx.setTabBarStyle({
        backgroundColor: "#a5646d",
      });
    } else if (theme == 2) {
      wx.setTabBarStyle({
        backgroundColor: "#1a1a1a",
      });
    } else if (theme == 3) {
      wx.setTabBarStyle({
        backgroundColor: "#86C6F6",
      });
    } else if (theme == 4) {
      wx.setTabBarStyle({
        backgroundColor: "#85a4cd",
      });
    }
    zoneMsg = wx.getStorageSync("xcx-zoneIntro") || "这是属于我们的小家";
    openid = wx.getStorageSync("openid") || "";
    this.setData({
      zoneMsg,
      openid,
    });
    this.setUser();
    this.getLengthData(openid);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
    clearInterval(this.data.getHunTime);
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    clearInterval(this.data.getHunTime);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() { },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() { },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    // return {
    //     title: '郭里星陈',
    //     path: '/page/user/user',
    //     imageUrl:'/static/icon/share.jpg'
    // }
  },
});
